<template>
  <div id='building'>
    <div class="login-box">
      <h3 class="name">后台购物管理系统</h3>
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="loginForm"
        label-width="60px"
        class="demo-loginForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            autocomplete="off"
           
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            autocomplete="off"             
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">提交</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
          <router-link to="/register"> <el-button type="warning" class="zhuce">注册账号</el-button></router-link>
        </el-form-item>
      </el-form>
      
    </div>
  </div>
</template>

<script>
import jwt from "jwt-decode";
import { mapMutations } from "vuex";
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { validator: validateUser, trigger: "blur" },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
        password: [
          { validator: validatePass, trigger: "blur" },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
      },
    };
  },
  methods: {
    ...mapMutations("loginModules", ["setUser"]),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let { username, password } = this.loginForm;
          // 请求登录接口
          this.$api
            .getLogin({
              username,
              password,
            })
            .then((res) => {
              console.log("登录成功", res.data);
              if (res.data.status === 200) {
                // console.log(jwt(res.data.data));
                //登录成功,存储登录信息 ;实现跳转 ;顶部区域显示用户信息 ；持久化
                let obj = {
                  user: jwt(res.data.data).username,
                  token: res.data.data,
                };
                this.setUser(obj);
                //存取到本地
                localStorage.setItem("user", JSON.stringify(obj));
                //跳转
                this.$router.push("/");
              } else {
                //账号或者密码错误
                this.$message.error("账号或者密码错误");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang='less' scoped>
.name {
  text-align: center;
  margin-bottom: 30px;
  font-size: 20px;
}
.login-box {
  width: 400px;
  height: 300px;
  padding: 20px;
  margin: 100px auto;
  margin-top: 190px;  
  background-color:rgba(255, 255, 255, 0.5);
}
#building{
  background:url("../../assets/images/2.jpg");
  width:100%;
  height:100%;
  position:fixed;
  background-size:100% 100%;
}
.zhuce{
  margin-left: 60px;
}
</style>